IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'RealmsGetSubNodesSummary')
	BEGIN
		DROP  Procedure  RealmsGetSubNodesSummary
	END

GO

CREATE Procedure RealmsGetSubNodesSummary
@SagaID Int,
@CreatorID int,
@RealmID Int = Null
AS
If(@RealmID IS Null)
Begin
	Select 
	r.Title,
	r.RealmID
	From Realm r
	Where r.SagaID = @SagaID And r.ISPrivate = 0 And r.CreatorID  <> @CreatorID
	Union All
		Select 
	r.Title,
	r.RealmID
	From Realm r
	Where r.SagaID = @SagaID And r.CreatorID  = @CreatorID
	
End
Else
	Begin
	Select 
	r.Title,
	r.RealmID
	From Realm r
	Where r.SagaID = @SagaID And r.ParentID = @RealmID And r.ISPrivate = 0 And r.CreatorID  <> @CreatorID
	Union All
	Select 
	r.Title,
	r.RealmID
	From Realm r
	Where r.SagaID = @SagaID And r.CreatorID  = @CreatorID  And r.ParentID = @RealmID
End
GO


